package cn.szzsi.service;

import cn.szzsi.common.model.OrderItem;
import cn.szzsi.common.model.Orders;
import cn.szzsi.common.model.Product;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengy on 2017/1/22.
 */
public class MemberOrderService {
    public static final MemberOrderService service = new MemberOrderService();
    public List<Object> getOrdersByType(Integer pageNumber, Integer pageSize, String memberId, String orderType){
        Page<Orders> pager;
        List<Object> list=new ArrayList<Object>();
        if("qbdd".equals(orderType)){//全部订单
            StringBuilder sql=new StringBuilder("from mall_orders where member_id = '"+memberId+"' order by createDate desc");
            pager = Orders.dao.paginate(pageNumber,pageSize,"select * ",sql.toString());
            for(int i=0;i<pager.getList().size();i++){
                Map<String,Object> map=new HashMap<String,Object>();//返回map集合
                map.put("type",orderType);
                //判断订单状态
                int orderStatus=pager.getList().get(i).getInt("orderStatus");//订单状态:0-未处理,1-已处理,2-已完成,3-已作废
                int paymentStatus=pager.getList().get(i).getInt("paymentStatus");//支付状态:0-未支付,1-部分支付,2-已支付,3-部分退款,4-全额退款,5-退款中
                int shippingStatus=pager.getList().get(i).getInt("shippingStatus");//物流状态:0-未发货,1-部分发货,2-已发货,3-部分退货,4-已退货,5-退货中
                if(orderStatus!=1 && orderStatus!=0 &&  shippingStatus!=5 && shippingStatus!=4 && shippingStatus!=3){
                    map.put("status","已完成");
                }else if(paymentStatus==0 && shippingStatus==0){
                    map.put("status","待付款");
                }else if(paymentStatus==2 && shippingStatus==0){
                    map.put("status","待发货");
                }else if(paymentStatus==2 && shippingStatus==2 && orderStatus!=2){
                    map.put("status","待收货");
                }else if(paymentStatus==5){
                    map.put("status","退款中");
                }else if(paymentStatus==3){
                    map.put("status","部分退款");
                }else if(paymentStatus==4){
                    map.put("status","全额退款");
                }else if (shippingStatus==5){
                    map.put("status","退货中");
                }else if(shippingStatus==4){
                    map.put("status","部分退货");
                }else if(shippingStatus==5){
                    map.put("status","已退货");
                }

                map.put("orderId",pager.getList().get(i).getStr("id"));
                map.put("orderSn",pager.getList().get(i).getStr("orderSn"));
                map.put("createDate",pager.getList().get(i).getDate("createDate"));
                map.put("totalAmount",pager.getList().get(i).getBigDecimal("totalAmount"));//总金额

                List<OrderItem> orderItemList=pager.getList().get(i).getOrderItemList();
                List<Product> products=new ArrayList();
                for(int j=0;j<orderItemList.size();j++){
                    products.add(orderItemList.get(j).getExceptProduct());
                    map.put("products",products);
                }
                list.add(map);
            }

        }else{
            StringBuilder sql=new StringBuilder();
            if ("dfk".equals(orderType)) {
                sql=new StringBuilder("from mall_orders where member_id = '"+memberId+"' and paymentStatus=0 and shippingStatus=0 order by createDate desc");
            }else if ("dfh".equals(orderType)){
                sql=new StringBuilder("from mall_orders where member_id = '"+memberId+"' and paymentStatus=2 and shippingStatus=0 order by createDate desc");
            }else if("dsh".equals(orderType) ){
                sql=new StringBuilder("from mall_orders where member_id = '"+memberId+"' and paymentStatus=2 and shippingStatus=2 and orderStatus!=2 order by createDate desc");
            }else if("dpj".equals(orderType) ){
                sql=new StringBuilder("from mall_orders where member_id = '"+memberId+"' and orderStatus =2 and orderEvaluate=0 order by createDate desc");
            }else if("thh".equals(orderType) ){//退换货
                sql=new StringBuilder("from mall_orders where member_id = '"+memberId+"' and (paymentStatus=5 or shippingStatus=5) order by createDate desc");
            }
            pager = Orders.dao.paginate(pageNumber,pageSize,"select * ",sql.toString());
            for(int i=0;i<pager.getList().size();i++){
                Map<String,Object> map=new HashMap<String,Object>();//返回map集合
                map.put("type",orderType);
                //判断订单状态
                int orderStatus=pager.getList().get(i).getInt("orderStatus");//订单状态:0-未处理,1-已处理,2-已完成,3-已作废
                int paymentStatus=pager.getList().get(i).getInt("paymentStatus");//支付状态:0-未支付,1-部分支付,2-已支付,3-部分退款,4-全额退款,5-退款中
                int shippingStatus=pager.getList().get(i).getInt("shippingStatus");//物流状态:0-未发货,1-部分发货,2-已发货,3-部分退货,4-已退货,5-退货中
                if("dpj".equals(orderType)){
                    map.put("status","待评价");
                }else if(paymentStatus==0 && shippingStatus==0){
                    map.put("status","待付款");
                }else if(paymentStatus==2 && shippingStatus==0){
                    map.put("status","待发货");
                }else if(paymentStatus==2 && shippingStatus==2 && orderStatus!=2){
                    map.put("status","待收货");
                }else if(paymentStatus==5){
                    map.put("status","退款中");
                }else if(paymentStatus==3){
                    map.put("status","部分退款");
                }else if(paymentStatus==4){
                    map.put("status","全额退款");
                }else if (shippingStatus==5){
                    map.put("status","退货中");
                }else if(shippingStatus==4){
                    map.put("status","部分退货");
                }else if(shippingStatus==5){
                    map.put("status","已退货");
                }
                map.put("orderId",pager.getList().get(i).getStr("id"));
                map.put("orderSn",pager.getList().get(i).getStr("orderSn"));
                map.put("createDate",pager.getList().get(i).getDate("createDate"));
                map.put("totalAmount",pager.getList().get(i).getBigDecimal("totalAmount"));//总金额

                List<OrderItem> orderItemList=pager.getList().get(i).getOrderItemList();
                List<Product> products=new ArrayList();
                for(int j=0;j<orderItemList.size();j++){
                    products.add(orderItemList.get(j).getExceptProduct());
                    map.put("products",products);
                }
                list.add(map);
            }
        }
        return list;
    }
}
